< previous page page_636 next page >

Page 636
Adaptive Depth Control
Program VBRay recursively follows a ray until either it does not intersect any objects in the scene or the ray-tracing routines reach a maximum depth of recursion. The program also stops following a ray if it can have no contribution to the color of the pixel whose value is being computed. For example, the program will not follow a refractive ray passing through a totally nontransparent object.
It will, however, follow a ray through a surface that is almost but not quite opaque. Each time a light ray reflects off a surface or passes through a transparent object, the possible contribution by the ray grows a bit smaller. The contribution due to a reflected ray, for example, is the value of the pixel where the ray intersects an object times the constant kr. If kr = 0.1, this contribution is fairly small. If the ray then reflects off another object with kr = 0.1, the new ray's contribution will be only 0.1 * 0.1 = 0.01 times the value of the pixel where the new ray intersects an object. After several reflections like this, the contribution due to the new light rays is undetectably small.
Instead of making your program run to some predefined depth of recursion, you can make it follow a light ray until the maximum contribution possible falls below some lower limit. When a ray the program is following cannot contribute significantly, the program should stop following it.
The simplest way to keep track of a ray's contribution is to change the depth parameter into the maximum contribution possible for a ray. Because the greatest value the red, green, and blue components of a ray's contribution can possibly have is 255, set the depth to 255 in your first call to the TraceRay subroutine.
Before you recursively invoke the TraceRay routine, reduce the depth parameter by a factor of the appropriate constant. If the new depth is less than one, the ray cannot contribute much to the original pixel's color, so you can ignore the ray. For example, the following code shows how you can update and check the depth parameter before following a reflective ray.
e10a6a0a59f035e870d90cca05b85fce.gif
factor = Krr
If Krg > factor Then factor = Krg
If Krb > factor Then factor = Krb
new_depth = depth * factor
If new_depth > 0 Then _
    TraceRay False, new_depth, target_object, _
        px, py, pz, vmx, vmy, vmz, _
        r_refl, g_refl, b_refl
With this new system in place, you can also reduce the value of the depth parameter as light passes through partially transparent objects. You can assign each object that is at least partially transparent a transparency coefficient. Then reduce the value of the depth parameter by this amount for every unit of distance a ray travels through the object. This makes a light ray weaker after it passes a long distance through a murky, translucent object.

 
< previous page page_636 next page >